package {{package}};

import {{invokerPackage}}.ApiClient;

{{#imports}}import {{import}};
{{/imports}}

import com.fasterxml.jackson.core.type.TypeReference;
import com.google.api.client.http.GenericUrl;
import com.google.api.client.http.HttpContent;
import com.google.api.client.http.InputStreamContent;
import com.google.api.client.http.HttpMethods;
import com.google.api.client.http.HttpResponse;
import com.google.api.client.json.Json;

import javax.ws.rs.core.UriBuilder;
import java.io.IOException;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.List;
import java.util.ArrayList;

{{>generatedAnnotation}}
{{#operations}}
public class {{classname}} {
    private ApiClient {{localVariablePrefix}}apiClient;

    public {{classname}}() {
        this(new ApiClient());
    }

    public {{classname}}(ApiClient apiClient) {
        this.{{localVariablePrefix}}apiClient = apiClient;
    }

    public ApiClient getApiClient() {
        return {{localVariablePrefix}}apiClient;
    }

    public void setApiClient(ApiClient apiClient) {
        this.{{localVariablePrefix}}apiClient = apiClient;
    }

    {{#operation}}
  /**{{#summary}}
    * {{summary}}{{/summary}}{{#notes}}
    * {{notes}}{{/notes}}{{#responses}}
    * <p><b>{{code}}</b>{{#message}} - {{message}}{{/message}}{{/responses}}{{#allParams}}
    * @param {{paramName}} {{description}}{{^description}}The {{paramName}} parameter{{/description}}{{/allParams}}{{#returnType}}
    * @return {{returnType}}{{/returnType}}
    * @throws IOException if an error occurs while attempting to invoke the API{{#externalDocs}}
    * {{description}}
    * @see <a href="{{url}}">{{summary}} Documentation</a>
    {{/externalDocs}}

    **/
    public {{#returnType}}{{{returnType}}} {{/returnType}}{{^returnType}}void {{/returnType}}{{operationId}}({{#allParams}}{{{dataType}}} {{paramName}}{{#hasMore}}, {{/hasMore}}{{/allParams}}) throws IOException {
        {{#returnType}}HttpResponse response = {{/returnType}}{{operationId}}ForHttpResponse({{#allParams}}{{paramName}}{{#hasMore}}, {{/hasMore}}{{/allParams}});{{#returnType}}
        TypeReference typeRef = new TypeReference<{{{returnType}}}>() {};
        return ({{{returnType}}})apiClient.getObjectMapper().readValue(response.getContent(), typeRef);{{/returnType}}
    }

  /**{{#summary}}
    * {{summary}}{{/summary}}{{#notes}}
    * {{notes}}{{/notes}}{{#responses}}
    * <p><b>{{code}}</b>{{#message}} - {{message}}{{/message}}{{/responses}}{{#requiredParams}}
    * @param {{paramName}} {{description}}{{^description}}The {{paramName}} parameter{{/description}}{{/requiredParams}}
    * @param params Map of query params. A collection will be interpreted as passing in multiple instances of the same query param.{{#returnType}}
    * @return {{returnType}}{{/returnType}}
    * @throws IOException if an error occurs while attempting to invoke the API{{#externalDocs}}
    * {{description}}
    * @see <a href="{{url}}">{{summary}} Documentation</a>
    {{/externalDocs}}

    **/
    public {{#returnType}}{{{returnType}}} {{/returnType}}{{^returnType}}void {{/returnType}}{{operationId}}({{#bodyParam}}{{^required}}{{{dataType}}} {{paramName}}, {{/required}}{{/bodyParam}}{{#requiredParams}}{{{dataType}}} {{paramName}}{{#hasMore}}, {{/hasMore}}{{/requiredParams}}{{#hasRequiredParams}}, {{/hasRequiredParams}}Map<String, Object> params) throws IOException {
        {{#returnType}}HttpResponse response = {{/returnType}}{{operationId}}ForHttpResponse({{#bodyParam}}{{^required}}{{paramName}}, {{/required}}{{/bodyParam}}{{#requiredParams}}{{paramName}}{{#hasMore}}, {{/hasMore}}{{/requiredParams}}{{#hasRequiredParams}}, {{/hasRequiredParams}}params);{{#returnType}}
        TypeReference typeRef = new TypeReference<{{{returnType}}}>() {};
        return ({{{returnType}}})apiClient.getObjectMapper().readValue(response.getContent(), typeRef);{{/returnType}}
    }

    public HttpResponse {{operationId}}ForHttpResponse({{#allParams}}{{{dataType}}} {{paramName}}{{#hasMore}}, {{/hasMore}}{{/allParams}}) throws IOException {
        {{#allParams}}{{#required}}// verify the required parameter '{{paramName}}' is set
        if ({{paramName}} == null) {
            throw new IllegalArgumentException("Missing the required parameter '{{paramName}}' when calling {{operationId}}");
        }{{/required}}{{/allParams}}
        {{#hasPathParams}}
        // create a map of path variables
        final Map<String, Object> uriVariables = new HashMap<String, Object>();{{#pathParams}}
        uriVariables.put("{{baseName}}", {{{paramName}}});{{/pathParams}}
        {{/hasPathParams}}
        UriBuilder uriBuilder = UriBuilder.fromUri(apiClient.getBasePath() + "{{{path}}}");{{#hasQueryParams}}
        {{#queryParams}}
        if ({{paramName}} != null) {
            String key = "{{baseName}}";
            Object value = {{paramName}};
            if (value instanceof Collection) {
                uriBuilder = uriBuilder.queryParam(key, ((Collection) value).toArray());
            } else if (value instanceof Object[]) {
                uriBuilder = uriBuilder.queryParam(key, (Object[]) value);
            } else {
                uriBuilder = uriBuilder.queryParam(key, value);
            }
        }{{/queryParams}}{{/hasQueryParams}}

        String url = uriBuilder{{#hasPathParams}}.buildFromMap(uriVariables).toString();{{/hasPathParams}}{{^hasPathParams}}.build().toString();{{/hasPathParams}}
        GenericUrl genericUrl = new GenericUrl(url);

        HttpContent content = {{#isBodyAllowed}}apiClient.new JacksonJsonHttpContent({{#bodyParam}}{{paramName}}{{/bodyParam}}{{^bodyParam}}null{{/bodyParam}}){{/isBodyAllowed}}{{^isBodyAllowed}}null{{/isBodyAllowed}};
        return apiClient.getHttpRequestFactory().buildRequest(HttpMethods.{{httpMethod}}, genericUrl, content).execute();
    }{{#bodyParam}}

      public HttpResponse {{operationId}}ForHttpResponse({{#allParams}}{{#isBodyParam}}java.io.InputStream {{paramName}}{{/isBodyParam}}{{^isBodyParam}}{{{dataType}}} {{paramName}}{{/isBodyParam}}{{#hasMore}}, {{/hasMore}}{{/allParams}}, String mediaType) throws IOException {
          {{#allParams}}{{#required}}// verify the required parameter '{{paramName}}' is set
              if ({{paramName}} == null) {
              throw new IllegalArgumentException("Missing the required parameter '{{paramName}}' when calling {{operationId}}");
              }{{/required}}{{/allParams}}
          {{#hasPathParams}}
                  // create a map of path variables
                  final Map<String, Object> uriVariables = new HashMap<String, Object>();{{#pathParams}}
                      uriVariables.put("{{baseName}}", {{{paramName}}});{{/pathParams}}
          {{/hasPathParams}}
              UriBuilder uriBuilder = UriBuilder.fromUri(apiClient.getBasePath() + "{{{path}}}");{{#hasQueryParams}}
              {{#queryParams}}
              if ({{paramName}} != null) {
                  String key = "{{baseName}}";
                  Object value = {{paramName}};
                  if (value instanceof Collection) {
                    uriBuilder = uriBuilder.queryParam(key, ((Collection) value).toArray());
                  } else if (value instanceof Object[]) {
                    uriBuilder = uriBuilder.queryParam(key, (Object[]) value);
                  } else {
                    uriBuilder = uriBuilder.queryParam(key, value);
                  }
              }{{/queryParams}}{{/hasQueryParams}}

              String url = uriBuilder{{#hasPathParams}}.buildFromMap(uriVariables).toString();{{/hasPathParams}}{{^hasPathParams}}.build().toString();{{/hasPathParams}}
              GenericUrl genericUrl = new GenericUrl(url);

              HttpContent content = {{#bodyParam}}{{paramName}} == null ?
                apiClient.new JacksonJsonHttpContent(null) :
                new InputStreamContent(mediaType == null ? Json.MEDIA_TYPE : mediaType, {{paramName}}){{/bodyParam}};
              return apiClient.getHttpRequestFactory().buildRequest(HttpMethods.{{httpMethod}}, genericUrl, content).execute();
      }{{/bodyParam}}

    public HttpResponse {{operationId}}ForHttpResponse({{#bodyParam}}{{^required}}{{{dataType}}} {{paramName}}, {{/required}}{{/bodyParam}}{{#requiredParams}}{{{dataType}}} {{paramName}}{{#hasMore}}, {{/hasMore}}{{/requiredParams}}{{#hasRequiredParams}}, {{/hasRequiredParams}}Map<String, Object> params) throws IOException {
        {{#allParams}}{{#required}}// verify the required parameter '{{paramName}}' is set
        if ({{paramName}} == null) {
            throw new IllegalArgumentException("Missing the required parameter '{{paramName}}' when calling {{operationId}}");
        }{{/required}}{{/allParams}}
        {{#hasPathParams}}
        // create a map of path variables
        final Map<String, Object> uriVariables = new HashMap<String, Object>();{{#pathParams}}
        uriVariables.put("{{baseName}}", {{{paramName}}});{{/pathParams}}
        {{/hasPathParams}}
        UriBuilder uriBuilder = UriBuilder.fromUri(apiClient.getBasePath() + "{{{path}}}");

        // Copy the params argument if present, to allow passing in immutable maps
        Map<String, Object> allParams = params == null ? new HashMap<String, Object>() : new HashMap<String, Object>(params);{{#queryParams}}{{#required}}
        // Add the required query param '{{paramName}}' to the map of query params
        allParams.put("{{paramName}}", {{paramName}});{{/required}}{{/queryParams}}

        for (Map.Entry<String, Object> entry: allParams.entrySet()) {
            String key = entry.getKey();
            Object value = entry.getValue();

            if (key != null && value != null) {
                if (value instanceof Collection) {
                    uriBuilder = uriBuilder.queryParam(key, ((Collection) value).toArray());
                } else if (value instanceof Object[]) {
                    uriBuilder = uriBuilder.queryParam(key, (Object[]) value);
                } else {
                    uriBuilder = uriBuilder.queryParam(key, value);
                }
            }
        }

        String url = uriBuilder{{#hasPathParams}}.buildFromMap(uriVariables).toString();{{/hasPathParams}}{{^hasPathParams}}.build().toString();{{/hasPathParams}}
        GenericUrl genericUrl = new GenericUrl(url);

        HttpContent content = {{#isBodyAllowed}}apiClient.new JacksonJsonHttpContent({{#bodyParam}}{{paramName}}{{/bodyParam}}{{^bodyParam}}null{{/bodyParam}}){{/isBodyAllowed}}{{^isBodyAllowed}}null{{/isBodyAllowed}};
        return apiClient.getHttpRequestFactory().buildRequest(HttpMethods.{{httpMethod}}, genericUrl, content).execute();
    }


    {{/operation}}
}
{{/operations}}
